package cn.italkcloud.cph.helper;

import cn.italkcloud.cph.entity.dto.vos.CallDetailDTO;
import cn.italkcloud.cph.entity.dto.vos.ReportCustomerFeeDTO;
import cn.italkcloud.cph.entity.dto.vos.ReportGatewayFeeDTO;
import cn.italkcloud.cph.entity.req.GetCallDetailReq;
import cn.italkcloud.cph.entity.req.GetReportVosFeeReq;
import cn.italkcloud.cph.utils.CacheUtil;
import cn.italkcloud.cph.utils.DateUtil;
import cn.italkcloud.cph.utils.RandomUtil;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 接口请求缓存工具
 * <p>
 * Redis Key
 *
 * @author dominate
 * @since 2024/7/22
 **/
public class RequestCacheHelper {

    private static final String TEMP_REQUEST_CACHE_HEAD = "temp:request:";

    private static final String REPORT_CUSTOMER_FEE = TEMP_REQUEST_CACHE_HEAD + "report:customer-fee:";
    private static final String REPORT_VOS_GATEWAY_FEE = TEMP_REQUEST_CACHE_HEAD + "report:vos-gateway-fee:";

    private static final String VOS_CALL_DETAIL = TEMP_REQUEST_CACHE_HEAD + "vos:call-detail:";


    private static final int[] TIME_RANGE = {120, 300};

    private static final String PARAM_SPLIT = ":";

    private static int randOutTime() {
        return RandomUtil.getRandNum(TIME_RANGE[0], TIME_RANGE[1]);
    }

    public static void saveVosCallDetail(List<CallDetailDTO> detailList, GetCallDetailReq req) {
        CacheUtil.set(parseVosCallDetailKey(req), detailList, randOutTime());
    }

    public static boolean hasVosCallDetail(GetCallDetailReq req) {
        return CacheUtil.hasKey(parseVosCallDetailKey(req));
    }

    public static List<CallDetailDTO> getVosCallDetail(GetCallDetailReq req) {
        return CacheUtil.getList(parseVosCallDetailKey(req), CallDetailDTO.class);
    }

    private static String parseVosCallDetailKey(GetCallDetailReq req) {
        return VOS_CALL_DETAIL + req.getServerId() + PARAM_SPLIT + req.getPhone();
    }

    public static void saveReportCustomerFee(List<ReportCustomerFeeDTO> cacheData, GetReportVosFeeReq req) {
        CacheUtil.set(parseReportVosFeeKey(req, REPORT_CUSTOMER_FEE), cacheData, randOutTime());
    }

    public static boolean hasReportCustomerFee(GetReportVosFeeReq req) {
        return CacheUtil.hasKey(parseReportVosFeeKey(req, REPORT_CUSTOMER_FEE));
    }

    public static List<ReportCustomerFeeDTO> getReportCustomerFee(GetReportVosFeeReq req) {
        return CacheUtil.getList(parseReportVosFeeKey(req, REPORT_CUSTOMER_FEE), ReportCustomerFeeDTO.class);
    }

    public static void saveReportVosGatewayFee(List<ReportGatewayFeeDTO> cacheData, GetReportVosFeeReq req) {
        CacheUtil.set(parseReportVosFeeKey(req, REPORT_VOS_GATEWAY_FEE), cacheData, randOutTime());
    }

    public static boolean hasReportVosGatewayFee(GetReportVosFeeReq req) {
        return CacheUtil.hasKey(parseReportVosFeeKey(req, REPORT_VOS_GATEWAY_FEE));
    }

    public static List<ReportGatewayFeeDTO> getReportVosGatewayFee(GetReportVosFeeReq req) {
        return CacheUtil.getList(parseReportVosFeeKey(req, REPORT_VOS_GATEWAY_FEE), ReportGatewayFeeDTO.class);
    }

    private static String parseReportVosFeeKey(GetReportVosFeeReq req, String redisKey) {
        return redisKey + formatDay(req.getStartDay()) + PARAM_SPLIT + formatDay(req.getEndDay()) + PARAM_SPLIT + Arrays.toString(req.getAccounts());
    }

    private static long formatDay(long time) {
        return DateUtil.getDateBegin(new Date(time)).getTime();
    }


}
